x86emul: XSA-123 follow-up
cover x86emul: XSA-123 follow-up
1: drop unused "bigval" fields from struct operand 2: simplify asm() constraints
PATCH 1/2 x86emul: drop unused "bigval" fields from struct operand
xen/arch/x86/x86_emulate/x86_emulate.c +4 -10
@@ -313,17 +313,11 @@ struct operand {enum {OP_REG ,OP_MEM ,OP_IMM ,OP_NONE } type;unsigned int bytes; -/* Up to 128-byte operand value, addressable as ulong or uint32_t[]. */ -union { -unsigned long val; -uint32_t bigval[4 ]; - }; +/* Operand value. */ +unsigned long val; -/* Up to 128-byte operand value, addressable as ulong or uint32_t[]. */ -union { -unsigned long orig_val; -uint32_t orig_bigval[4 ]; - }; +/* Original operand value. */ +unsigned long orig_val;union {/* OP_REG: Pointer to register field. */
x86emul: drop unused "bigval" fields from struct operand
xen/arch/x86/x86_emulate/x86_emulate.c +4 -10
@@ -313,17 +313,11 @@ struct operand {enum {OP_REG ,OP_MEM ,OP_IMM ,OP_NONE } type;unsigned int bytes; -/* Up to 128-byte operand value, addressable as ulong or uint32_t[]. */ -union { -unsigned long val; -uint32_t bigval[4 ]; - }; +/* Operand value. */ +unsigned long val; -/* Up to 128-byte operand value, addressable as ulong or uint32_t[]. */ -union { -unsigned long orig_val; -uint32_t orig_bigval[4 ]; - }; +/* Original operand value. */ +unsigned long orig_val;union {/* OP_REG: Pointer to register field. */
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH 1/2] x86emul: drop unused "bigval" fields from struct operand
Quoted 1 line · Jan Beulich
Signed-off-by Jan Beulich <[email protected]>
PATCH 2/2 x86emul: simplify asm() constraints
In reply to Jan BeulichUse + on outputs instead of = and a matching input. Allow not just memory for the _eflags operand (it turns out that recent gcc produces worse code when also doing this for _dst.val, so the latter is being avoided).
xen/arch/x86/x86_emulate/x86_emulate.c +25 -31
@@ -428,7 +428,7 @@ typedef union {/* Before executing instruction: restore necessary bits in EFLAGS. */ #define _PRE_EFLAGS (_sav, _msk, _tmp) \/* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \ -"movl %"_sav",%"_LO32 _tmp"; " \ +"movl %"_LO32 _sav",%"_LO32 _tmp"; " \ "push %"_tmp"; " \ "push %"_tmp"; " \ "movl %"_msk",%"_LO32 _tmp"; " \
@@ -448,7 +448,7 @@ typedef union {"pushf; " \"pop %" _tmp"; " \"andl %" _msk",%" _LO32 _tmp"; " \ -"orl %" _LO32 _tmp",%" _sav"; " +"orl %" _LO32 _tmp",%" _LO32 _sav"; " /* Raw emulation: instruction has two explicit operands. */ #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy)\
@@ -460,18 +460,16 @@ do{ unsigned long _tmp; _PRE_EFLAGS("0","4","2") \ _op"w %" _wx"3,%1; " \ _POST_EFLAGS("0" ,"4" ,"2" ) \ - : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \ -: _wy ((_src).val), " i" (EFLAGS_MASK), \ -" m" (_eflags), " m" ((_dst).val) ); \ + : "+g" (_eflags), "+m" ((_dst).val), "=&r" (_tmp) \ +: _wy ((_src).val), " i" (EFLAGS_MASK) ); \ break; \ case 4: \ asm volatile ( \ _PRE_EFLAGS("0","4","2") \ _op"l %"_lx"3,%1; " \ _POST_EFLAGS("0","4","2") \ - : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \ -: _ly ((_src).val), " i" (EFLAGS_MASK), \ - "m" (_eflags), "m" ((_dst).val) ); \ + : "+g" (_eflags), "+m" ((_dst).val), "=&r" (_tmp) \ +: _ly ((_src).val), " i" (EFLAGS_MASK) ); \ break; \ case 8: \ __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy); \
@@ -487,9 +485,8 @@ do{ unsigned long _tmp; _PRE_EFLAGS("0","4","2") \ _op"b %" _bx"3,%1; " \ _POST_EFLAGS("0" ,"4" ,"2" ) \ - : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \ -: _by ((_src).val), " i" (EFLAGS_MASK), \ -" m" (_eflags), " m" ((_dst).val) ); \ + : "+g" (_eflags), "+m" ((_dst).val), "=&r" (_tmp) \ +: _by ((_src).val), " i" (EFLAGS_MASK) ); \ break; \ default: \ __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy);\
@@ -519,24 +516,24 @@ do{ unsigned long _tmp; _PRE_EFLAGS("0","3","2") \ _op"b %1; " \ _POST_EFLAGS("0" ,"3" ,"2" ) \ - : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \ -: " i" (EFLAGS_MASK), " m" (_eflags), " m" ((_dst).val) ); \ + : "+g" (_eflags), "+m" ((_dst).val), "=&r" (_tmp) \ +: " i" (EFLAGS_MASK) ); \ break; \ case 2: \ asm volatile ( \ _PRE_EFLAGS("0","3","2") \_op" w %1 ;" \ _POST_EFLAGS("0","3","2") \ - : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \ -: " i" (EFLAGS_MASK), " m" (_eflags), " m" ((_dst).val) ); \ + : "+g" (_eflags), "+m" ((_dst).val), "=&r" (_tmp) \ +: " i" (EFLAGS_MASK) ); \ break; \ case 4: \ asm volatile ( \ _PRE_EFLAGS("0","3","2") \_op" l %1 ;" \ _POST_EFLAGS("0","3","2") \ - : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \ - : "i" (EFLAGS_MASK), "m" (_eflags), "m" ((_dst).val) ); \ + : "+g" (_eflags), "+m" ((_dst).val), "=&r" (_tmp) \ +: " i" (EFLAGS_MASK) ); \ break; \ case 8: \ __emulate_1op_8byte(_op, _dst, _eflags); \
@@ -551,17 +548,16 @@ do{ asm volatile ( _PRE_EFLAGS("0","4","2") \ _op"q %" _qx"3,%1; " \ _POST_EFLAGS("0" ,"4" ,"2" ) \ - : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \ - : _qy ((_src).val), "i" (EFLAGS_MASK), \ - "m" (_eflags), "m" ((_dst).val) ); \ + : "+g" (_eflags), "+m" ((_dst).val), "=&r" (_tmp) \ + : _qy ((_src).val), "i" (EFLAGS_MASK) ); \ } while (0) #define __emulate_1op_8byte(_op, _dst, _eflags) \ do{ asm volatile ( \ _PRE_EFLAGS("0","3","2") \ _op"q %1; " \ _POST_EFLAGS("0","3","2") \ - : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \ - : "i" (EFLAGS_MASK), "m" (_eflags), "m" ((_dst).val) ); \ + : "+g" (_eflags), "+m" ((_dst).val), "=&r" (_tmp) \ + : "i" (EFLAGS_MASK) ); \ } while (0)#elif defined(__i386__)#define __emulate_2op_8byte (_op, _src, _dst, _eflags, _qx, _qy)
@@ -806,9 +802,9 @@ static int read_ulong(static bool_t mul_dbl (unsigned long m[2 ]) {bool_t rc; - asm ("mul %4; seto %b2" - :"=a" (m[0 ]),"=d" (m[1 ]),"=q" (rc) - :"0" (m[0 ]),"1" (m[1 ]),"2" (0 ) ); + asm ("mul %1; seto %b2" + :"+a" (m[0 ]),"+d" (m[1 ]),"=q" (rc) + :"2" (0 ) );return rc; }
@@ -820,9 +816,9 @@ static bool_t mul_dbl(unsigned long m[2]static bool_t imul_dbl (unsigned long m[2 ]) {bool_t rc; - asm ("imul %4; seto %b2" - :"=a" (m[0 ]),"=d" (m[1 ]),"=q" (rc) - :"0" (m[0 ]),"1" (m[1 ]),"2" (0 ) ); + asm ("imul %1; seto %b2" + :"+a" (m[0 ]),"+d" (m[1 ]),"=q" (rc) + :"2" (0 ) );return rc; }
@@ -836,9 +832,7 @@ static bool_t div_dbl(unsigned long u[2] {if ( (v ==0 ) || (u[1 ] >= v) )return 1 ; - asm ("div %4" - :"=a" (u[0 ]),"=d" (u[1 ]) - :"0" (u[0 ]),"1" (u[1 ]),"r" (v) ); + asm ("div %2" :"+a" (u[0 ]),"+d" (u[1 ]) :"r" (v) );return 0 ; }
x86emul: simplify asm() constraints
Use + on outputs instead of = and a matching input. Allow not just memory for the _eflags operand (it turns out that recent gcc produces worse code when also doing this for _dst.val, so the latter is being avoided).
xen/arch/x86/x86_emulate/x86_emulate.c +25 -31
134 lines not on this page · raw message
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH 2/2] x86emul: simplify asm() constraints
Quoted 28 lines · Jan Beulich
Use + on outputs instead of = and a matching input. Allow not just memory for the
_eflagsoperand (it turns out that recent gcc produces worse code when also doing this for _dst.val, so the latter is being avoided).Signed-off-by Jan Beulich <[email protected]>xen/arch/x86/x86_emulate/x86_emulate.c +2
-2@@ -428,7 +428,7 @@ typedef union {/* Before executing instruction: restore necessary bits in EFLAGS. */ #define _PRE_EFLAGS (_sav, _msk, _tmp) \/* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \ -"movl %"_sav",%"_LO32 _tmp"; " \ +"movl %"_LO32 _sav",%"_LO32 _tmp"; " \ "push %"_tmp"; " \ "push %"_tmp"; " \ "movl %"_msk",%"_LO32 _tmp"; " \@@ -448,7 +448,7 @@ typedef union {"pushf; " \"pop %" _tmp"; " \"andl %" _msk",%" _LO32 _tmp"; " \ -"orl %" _LO32 _tmp",%" _sav"; " +"orl %" _LO32 _tmp",%" _LO32 _sav"; " /* Raw emulation: instruction has two explicit operands. */ #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy)\
xen/arch/x86/x86_emulate/x86_emulate.c do{ unsigned long _tmp; · line 464
Quoted 8 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2
-3@@ -460,18 +460,16 @@ do{ unsigned long _tmp; _PRE_EFLAGS("0","4","2") \ _op"w %" _wx"3,%1; " \ _POST_EFLAGS("0" ,"4" ,"2" ) \ - : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \ -: _wy ((_src).val), " i" (EFLAGS_MASK), \ -" m" (_eflags), " m" ((_dst).val) ); \ + : "+g" (_eflags), "+m" ((_dst).val), "=&r" (_tmp) \ +: _wy ((_src).val), " i" (EFLAGS_MASK) ); \
I believe the old ASM was buggy, not just inefficient.
Having read the Extended ASM documentation quite carefully, the following statement is relevant
"Only input operands may use numbers in constraints, and they must each refer to an output operand. Only a number (or the symbolic assembler name) in the constraint can guarantee that one operand is in the same place as another. The mere fact tha|t 'foo' |||is the value of both operands is not enough to guarantee that they are in the same place in the generated assembler code."
Because the input operands do not use numbers, the asm must read from %5 and write to %0 to guarantee that the _eflags temporary is used properly.
I believe that this transformation does now make the asm correct, as the output and input sides are now guaranteed to match the %0 used to reference the _eflags temporary.
Did you observe any code changes simply from changing = constraints to +, or did we get very lucky in with the generated code?
I think it might be a very wise idea to switch to using symbolic names. This code is very complicated and has many ways to go subtly wrong.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH 2/2] x86emul: simplify asm() constraints
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 10.03.15 at 20:48, <[email protected]> wrote:
Did you observe any code changes simply from changing = constraints to +, or did we get very lucky in with the generated code?
I think it's not really unexpected that there was no change, since the operands were all memory-only ones (and generally gcc avoids to address the same memory location in multiple different ways).
Quoted 2 lines
I think it might be a very wise idea to switch to using symbolic names. This code is very complicated and has many ways to go subtly wrong.
Let's save this for another day.
Jan
Re: [PATCH 2/2] x86emul: simplify asm() constraints
In reply to Jan Beulichxen/arch/x86/x86_emulate/x86_emulate.c static int read_ulong( · line 807
Quoted 10 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +3
-3@@ -806,9 +802,9 @@ static int read_ulong(static bool_t mul_dbl (unsigned long m[2 ]) {bool_t rc; - asm ("mul %4; seto %b2" - :"=a" (m[0 ]),"=d" (m[1 ]),"=q" (rc) - :"0" (m[0 ]),"1" (m[1 ]),"2" (0 ) ); + asm ("mul %1; seto %b2" + :"+a" (m[0 ]),"+d" (m[1 ]),"=q" (rc) + :"2" (0 ) );
Would 'bool_t rc = 0' allow you to switch operand 2 to +q and drop the last input operand as well? Or did that also produce worse code?
Cheers,
Tim.
Re: [PATCH 2/2] x86emul: simplify asm() constraints
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 12.03.15 at 11:42, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c static int read_ulong( · line 807
Quoted 10 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +3
-3@@ -806,9 +802,9 @@ static int read_ulong(static bool_t mul_dbl (unsigned long m[2 ]) {bool_t rc; - asm ("mul %4; seto %b2" - :"=a" (m[0 ]),"=d" (m[1 ]),"=q" (rc) - :"0" (m[0 ]),"1" (m[1 ]),"2" (0 ) ); + asm ("mul %1; seto %b2" + :"+a" (m[0 ]),"+d" (m[1 ]),"=q" (rc) + :"2" (0 ) );Would '
bool_trc = 0' allow you to switch operand 2 to +q and drop the last input operand as well?
Yes.
Quoted 1 line
Or did that also produce worse code?
I didn't try yet, but I can't see why it would.
Jan